﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace MicroFormatProcessing
{
    /// <summary>
    /// User Queries
    /// </summary>

    public class UserRepository
    {
        private static CloudStorageAccount storageAccount;
        private UserContext context;

        static UserRepository()
        {
            CloudStorageAccount.SetConfigurationSettingPublisher(
               (configName, configSetter) =>
                       configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));
 
            storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(UserContext.TABLE_NAME);
            // CloudTableClient.CreateTablesFromModel(typeof(MfUserTagContext), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
        }

        public UserRepository()
        {
            this.context = new UserContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
            this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
            this.context.IgnoreResourceNotFoundException = true;
        }

        public IEnumerable<User> Select(string mfUser)
        {
            return CreateQuery((from mfu in this.context.MfUsers
                                where mfu.PartitionKey == mfUser
                                select mfu).AsTableServiceQuery<User>()).Execute();
        }

        public List<User> SelectAll() {
            List<User> result = new List<User>();
            HashSet<string> references = new HashSet<string>();
            CloudTableQuery<User> query;
            query = CreateQuery((from mfu in this.context.MfUsers
                                select mfu).AsTableServiceQuery<User>());

            foreach (User user in query.Execute()) {
                result.Add(user);
            }
            return result;
        }



        public void Add(User mfUser)
        {
            this.context.Add(mfUser);
        }

        public void Update(User mfUser)
        {
            this.context.Update(mfUser);
        }

        public bool HasUsers()
        {
            return (this.context.MfUsers.FirstOrDefault() != null);
        }

        private CloudTableQuery<User> CreateQuery(CloudTableQuery<User> query)
        {
            if (HasUsers())
            {
                return query;
            }
            else
            {
                return (from mfu in this.context.MfUsers
                        select mfu).AsTableServiceQuery<User>();
            }
        }
    }

}
